<?php

namespace zhtfrontend\modules\teachergrowth;

use yii\web\UrlManager;
use yii\helpers\Json;

class Module extends \yii\base\Module
{
    public $controllerNamespace = 'zhtfrontend\modules\teachergrowth\controllers';

    public function init()
    {
        parent::init();

        if (strpos(\Yii::$app->request->getUrl(), 'teachergrowth')) {
            \Yii::$app->set('urlManager', [
                'class' => UrlManager::className(),
                'enablePrettyUrl' => true,
                //'enableStrictParsing' => true,
                'showScriptName' => false,
                'rules' => [
                    [
                        'class' => 'yii\rest\UrlRule',
                        'controller' => [
                        ]
                    ],
                ],
            ]);
            \Yii::$app->set('response', [
                'class' => 'yii\web\Response',
                'on beforeSend' => function ($event) {
                    $response = $event->sender;
                    if ($response->data !== null) {
                        if ($response->getStatusCode() == 200) {
                            if (isset($response->data['items'])) {
                                if (isset($response->data['_links']) && isset($response->data['_meta'])) {
                                    $response->data = [
                                        'statusCode' => $response->getStatusCode(),
                                        'message' => 'success',
                                        'items' => $response->data['items'],
                                        '_links' => $response->data['_links'],
                                        '_meta' => $response->data['_meta']
                                    ];
                                } else {
                                    $response->data = [
                                        'statusCode' => $response->getStatusCode(),
                                        'message' => 'success',
                                        'items' => $response->data['items'],
                                    ];
                                }
                            }
                        } else {
                            $response->data = [
                                'statusCode' => $response->getStatusCode(),
                                'message' => 'error',
                                'items' => $response->data['message'],
                            ];
                        }
                    }
                },
            ]);
        }
    }


    public function beforeAction($action)
    {
        if (\Yii::$app->getRequest()->method == 'GET') {
            //$data = Json::decode(base64_decode(file_get_contents("php://input")));
            $data = Json::decode(file_get_contents("php://input"));
            \Yii::$app->getRequest()->setBodyParams($data);
        } elseif (\Yii::$app->getRequest()->method == 'PUT') {

        } elseif (\Yii::$app->getRequest()->method == 'DELETE') {

        }
        return parent::beforeAction($action); // TODO: Change the autogenerated stub
    }
}
